package com.demo.listener;

import com.demo.pojo.User;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RabbitListener(queues = {"hello-queue"})
@Component
@Slf4j
public class listener1 {

    @RabbitHandler
    public void listener(User user, Channel channel, Message message) throws IOException {
        try {
            System.out.println(user);
            System.out.println(message);
            //int i = 1/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 修改失败 拒绝消息 使消息重新入队
            //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            log.error("消费失败");
            //消费失败，重新入队
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            //消费失败，直接舍弃
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);

        }
    }
}
